package com.zh.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zh.springcloud.common.Result;
import com.zh.springcloud.feign.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @ClassName OrderController
 * @date: 2021.06.09 11:17
 * @Author: zhanghang
 */
@RestController
@Slf4j
public class OrderFeignController {

	// 将feign接口注入进来
	@Autowired
	private PaymentService paymentService;

	@GetMapping("/test")
	public String test(){
		return "test-ok";
	}

	@GetMapping("/consumer/payment/hystrix/ok/{id}")
	public Result<?> paymentinfo_ok(@PathVariable Integer id){
		Result<Object> result = new Result<>();
		// 可以直接使用
		String str = paymentService.paymentinfo_ok(id);
		result.setResult(str);
		return result;
	}

	@GetMapping("/consumer/payment/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "paymentinfo_timeoutFallbackMethod", commandProperties =
		@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
	)
	public Result<?> paymentinfo_timeout(@PathVariable Integer id){
		Result<Object> result = new Result<>();
		// 可以直接使用
//		int a = 10 / 0;
		String str = paymentService.paymentinfo_timeout(id);
		result.setResult(str);
		return result;
	}

	public Result<?> paymentinfo_timeoutFallbackMethod(@PathVariable("id") Integer id){
		Result<Object> result = new Result<>();
		String str ="我是消费者80，对方服务繁忙，请稍后再试！"+id;
		result.setResult(str);
		return result;
	}
}
